package tianwang.dao;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import tianwang.model.WordsDaoEntry;
import tianwang.util.IbatisUtil;

import com.ibatis.sqlmap.client.SqlMapClient;

public class WordLevelDao {
	private SqlMapClient sqlMap = null;
	
	public WordsDaoEntry getWord(String entry) throws IOException, SQLException{
		if(sqlMap == null){
			this.sqlMap = IbatisUtil.getCocaSqlMapClient();
		}
		
		List<WordsDaoEntry> tmpList = this.sqlMap.queryForList("Word.getWrodLevel", entry);
		
		if(tmpList == null || tmpList.size() == 0){
			return null;
		}
		
		WordsDaoEntry minEntry = tmpList.get(0);
		Iterator<WordsDaoEntry> it = tmpList.iterator();
		while(it.hasNext()){
			WordsDaoEntry aEntry = it.next();
			if(aEntry.getLevel() < minEntry.getLevel()){
				minEntry = aEntry;
			}
		}
		
		return minEntry;
	}
}
